﻿#region

using System;
using CuongHanh.Configuration;
using CuongHanh.DAL;
using CuongHanh.Helpers;
using CuongHanh.Models;
using CuongHanh.Models.NotMapDB;

#endregion

namespace CuongHanh.Service
{
    public class CustomerServce
    {
        public static ChangePasswordByForgot GetCustomerFromResetKey(string resetKey)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer model = unitOfWork.FirstOrDefault<Customer>(c => c.ResetKey == resetKey);
                return model != null
                        ? new ChangePasswordByForgot(model)
                        : new ChangePasswordByForgot();
            }
        }

        public static Customer Login(CustomerLogin model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Username == model.Username);
                if(oldModel != null
                   && oldModel.Password == PasswordHelper.GetPassword(model.Username,
                           model.Password))
                {
                    return oldModel;
                }
                return null;
            }
        }

        public static bool Insert(Customer model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Username == model.Username || c.Email == model.Email);
                if(oldModel != null)
                    return false;

                unitOfWork.Insert(model);
                unitOfWork.Save();
                return true;
            }
        }

        public static bool Update(Customer model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Id == model.Id);
                if(model != null)
                {
                    oldModel.UpdateInfo(model);
                    unitOfWork.Save();
                    return true;
                }
                return false;
            }
        }

        public static bool ForgotPassword(ResetPassWord model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Email == model.Email);
                if(oldModel == null)
                    return false;
                oldModel.TimeReset = DateTime.Now;
                oldModel.ResetKey = PasswordHelper.GetResetKey(oldModel.Id,
                        oldModel.Username);
                oldModel.LastModified = DateTime.Now;
                unitOfWork.Save();
                return true;
            }
        }

        public static bool ChangePasswordByForgot(ChangePasswordByForgot model)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Id == model.Id && c.ResetKey == model.ResetKey && c.TimeReset.HasValue);
                if(oldModel == null)
                    return false;
                oldModel.Password = PasswordHelper.GetPassword(oldModel.Username,
                        model.Password);
                oldModel.ResetKey = string.Empty;
                oldModel.TimeReset = null;
                unitOfWork.Save();
                return true;
            }
        }

        public static bool ChangePassword(ChangePassword model,int customerId)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                Customer oldModel = unitOfWork.FirstOrDefault<Customer>(c => c.Id == customerId);
                if (oldModel == null || oldModel.Password != PasswordHelper.GetPassword(oldModel.Username,model.OldPassword))
                    return false;
                oldModel.Password = PasswordHelper.GetPassword(oldModel.Username,
                        model.Password);
                oldModel.LastModified = DateTime.Now;
                unitOfWork.Save();
                return true;
            }
        }

    }
}
